package com.example.util;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by simon on 2018/4/30.
 */
public class AipUtil {

    public static final String APP_ID = "11173770";
    public static final String API_KEY = "7XCACks4OM4wXNKUFBhOHyik";
    public static final String SECRET_KEY = "8AlIvsBPWnQlvOMrMQh1VwSStUwgvDVY";

    private final static Logger logger = LoggerFactory.getLogger(AipUtil.class);

    /**
     * 获取下次检验日期
     * @param path
     * @return
     */
    public static String getNextCheckDate(String path) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");
        try{
            JSONObject res = client.basicGeneral(path, options);
            JSONArray aa = (JSONArray) res.get("words_result");
            for (int i=0; i<aa.length(); i++){
                if((((JSONObject)aa.get(i)).get("words")+"").contains("下次检验日期")){
                    String regEx="[^0-9]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher((((JSONObject)aa.get(i)).get("words")+""));
                    String date = m.replaceAll("").trim();
                    if(null!=date && date.length()==6){
                        if(date.substring(4,6).equals("02")){
                            date = date+"28";
                        }else{
                            date = date+"30";
                        }
                    }
                    return date;
                }
            }
        }catch(Exception e){
            logger.error("通用文字识别失败",e);
            return null;
        }
        return null;
    }

    /**
     * 获取下次检验日期
     * @param path
     * @return
     */
    public static Map<String, Object> checkBussinessImg(String path, String partyName, String corporation, String creditCode) {
        // 初始化一个AipOcr

        Map resMap = new HashMap();
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "false");
        options.put("detect_language", "false");
        options.put("probability", "false");
        try{
            JSONObject res = client.basicGeneral(path, options);
            JSONArray aa = (JSONArray) res.get("words_result");
            logger.info("校验营业执照，扫描获得的结果："+aa.toString());
            boolean hasPartyName = false;
            boolean hasCorporation = false;
            boolean hasCreditCode = true;
            for (int i=0; i<aa.length(); i++){
                if( !hasPartyName && (((JSONObject)aa.get(i)).get("words")+"").contains(partyName)){
                    hasPartyName = true;
                }
                if(!hasCorporation && (((JSONObject)aa.get(i)).get("words")+"").contains(corporation)){
                    hasCorporation = true;
                }
//                String wd = ((JSONObject)aa.get(i)).get("words")+"";
//                if(wd.contains("信用代码")){
//                    wd = wd.replaceAll("0","O");
//                    creditCode = creditCode.replaceAll("0","O");
//                }
//                if(!hasCreditCode && wd.contains(creditCode)){
//                    hasCreditCode = true;
//                }
            }
            resMap.put("hasPartyName", hasPartyName);
            resMap.put("hasCorporation", hasCorporation);
            resMap.put("hasCreditCode", hasCreditCode);

            return resMap;
        }catch(Exception e){
            logger.error("通用文字识别失败",e);
            return null;
        }
    }

}
